<?php
class CommentsController extends AppController {
    	var $name = 'Comments';
        var $components = array('RequestHandler', 'Auth');
var $helpers = array('Js');

//        function add($post_id = null) {
//            	if (!empty($this->data)) {
//                       
//			if ($this->Comment->saveAll($this->data)) {
//				$this->Session->setFlash($this->data['Comment']['id']);
//				$this->redirect(array('action' => '../posts/index'));
//			}
//		}
//            }

//        var $components = array('Session');

    function beforeFilter()
    {
        $this->Auth->allow('add', 'view');
        parent::beforeFilter();
    }  
    
    function sechungpark(&$com) {
        foreach ($com as &$com1) {
            if (isset($com1['Comment'])) {
            $com1['Comment']['replies'] = $this->Comment->find('all', array('conditions' => array('Comment.parent_id' => $com1['Comment']['id']), 'order' => array('Comment.created DESC'), 'fields' => array('Comment.text', 'Comment.id', 'Comment.name', 'Comment.email', 'Comment.up', 'Comment.down', 'Comment.anonid'), 'contain'=>'Tag'));
                if (count($com1['Comment']['replies']) > 0)
                    $this->sechungpark($com1['Comment']['replies']);
                else
                    unset($com1['Comment']['replies']);
            }
        }
    }
  
    function view($id=null)
    {
//       $allCategories = $this->Category->find('threaded');
     $com=  $this->Comment->find('all', array('conditions' => array('Comment.id' => $id), 'order' => array('Comment.created DESC'), 'fields' => array('Comment.text', 'Comment.id', 'Comment.name', 'Comment.email', 'Comment.up', 'Comment.down', 'Comment.anonid'), 'contain'=>'Tag'));
       $this->sechungpark($com);
      $this->set('com', $com);
//        $this->set('com1', $com);
//       $someCategories = $this->Category->find('threaded', array(
//            'conditions' => array(
//                    'Article.lft >=' => $aCategory['Category']['lft'], 
//                    'Article.rght <=' => $aCategory['Category']['rght']
//            )
//       ));
     }
    
     function add($id=null, $sort="Comment.created") {
        if (!empty($this->data)) {
           unset($this->data['Post']);
           $this->data['Post']['id']=$id;
           if ($this->data['Comment']['name']=='')
               $this->data['Comment']['name']='Anonymous';
           $ip= long2ip($_SERVER['REMOTE_ADDR']);
           $ip= mt_rand(0, 65537) * mt_rand(0, 65535);
           $ip=82645934;
           $this->data['Comment']['ip']=$ip;
           $test=$this->Comment->find('first', array('conditions' => array('post_id' => $id, 'ip'=>$ip)));
           if ($test==false) { 
               $this->data['Comment']['anonid']=curl_download('http://www.random.org/strings/?num=1&len=6&digits=on&upperalpha=on&loweralpha=on&unique=on&format=plain&rnd=');
           }
           else {
               $this->data['Comment']['anonid']=$test['Comment']['anonid'];
               }
               
            if ($this->Comment->saveAll($this->data)) {
                 $this->layout="none";
//                $this->Session->setFlash(print_r($this->data));
//                if (file_exists("comments/".$this->data['Post']['id'].".html"))
//                     unlink("comments/".$this->data['Post']['id'].".html");
                $nid=$this->data['Post']['id'];
               
                //SET!
//              print_r($this->data);
                $this->redirect('/posts/onchange/'.$nid.'/'.$sort.'/'.$this->Comment->id);
                
            } else {
                
            }
        }
    }
    function delete()
    {
        }
    
    function up($commentId=null)
    {
//        $this->view = 'Json';
//        $info = $this->MyModel->getSomeInfo();
//        $json = array(
//          'variableOne' =>'dy'
//        );
//        $this->set(compact('json'));
        $this->autoRender = $this->layout = false; 
        if ($commentId)
            {
//            $this->Session->setFlash('Comment '.$commentId.' upvoted');
//            $this->Comment->id=$commentId;
            //$this->Comment->updateAll($fields, $conditions); 
//            this->Comment->updateAll(array('Baker.approved' => true), array('Comment.up'));
           
            $this->Comment->updateAll( array('Comment.up' => 'Comment.up + 1'), array('Comment.id' =>$commentId));  
        
//            $this->data = $this->Comment->read();
//            $this->data['Comment']['up']++;
//            $this->Comment->save($this->data);
            }
        }
    function down($commentId=null)
    {
        if ($commentId)
            $this->Comment->updateAll( array('Comment.down' => 'Comment.down + 1'), array('Comment.id' =>$commentId));  
     }
   
     
}

function curl_download($Url){

    // is cURL installed yet?
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }

    // OK cool - then let's create a new cURL resource handle
    $ch = curl_init();

    // Now set some options (most are optional)

    // Set URL to download
    curl_setopt($ch, CURLOPT_URL, $Url);

    // Set a referer
    curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");

    // User agent
    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

    // Include header in result? (0 = yes, 1 = no)
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // Should cURL return or print out the data? (true = return, false = print)
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    // Download the given URL, and return output
    $output = curl_exec($ch);

    // Close the cURL resource, and free system resources
    curl_close($ch);

    return $output;
}
?>
